package com.ld.hpfs.web.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ld.hpfs.common.utils.Const;
import com.ld.hpfs.service.RuleService;
import com.ld.hpfs.web.interseptor.WebConstants;

/**
 * 
 * @author Frank Luo
 * 
 */
@Controller
@RequestMapping("/io/rule")
public class RuleExportController {

	@Autowired
	private RuleService ruleService;

	// 模板路径
	private String strPath = Const.REAL_PATH.substring(0,
			Const.REAL_PATH.lastIndexOf("\\tomcat-7.0"))
			+ "\\reports\\rules\\";

	// 输入流
	private InputStream is;

	// 输出流
	private ServletOutputStream out;

	// POI输入流
	private POIFSFileSystem poifs;

	// 工作空间
	private HSSFWorkbook workbook;

	// 规则主数据数据
	private ArrayList<HashMap<String, Object>> rule_main_data;

	// 规则条件
	private ArrayList<HashMap<String, Object>> rule_condition_data;

	// 规则后调整
	private ArrayList<HashMap<String, Object>> adjust_after_rule_data;
	
	// 当前语言环境
	private String lang = "";
	/**
	 * 导出模板
	 * 
	 * @param Id_Rule
	 *            所有要导出数据的ruleId
	 * @param type
	 *            导出的规则类型
	 * @param session
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("null")
	@RequestMapping(value = "/SetRule/{type}/{cust_code}/{creator}/download", method = RequestMethod.GET)
	public void SetRuleDownload(@PathVariable String type,
			@PathVariable String cust_code, 
			@PathVariable String creator, HttpSession session,
			HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/x-download");
		int rule_type = Integer.parseInt(type);
		// 获得语言环境
		String language = (String) session.getAttribute(WebConstants.CURRENT_LANG);
		lang = language;
		HashMap<String, Object> listParam = new HashMap<String, Object>();
		listParam.put("type", type);
		// 客户代码
		if (! "emptyCode".equals(cust_code)) {
			cust_code = "%" + cust_code + "%";
			listParam.put("cust_code", cust_code);
		} else{
			cust_code=null;
		}
		// 创建者参数
		if (!"emptyCreator".equals(creator)) {
			creator = "%" + creator + "%";
			listParam.put("creator", creator);
		} else{
			creator=null;
		}
		// 公司ID
		listParam.put("companyId",
				session.getAttribute(WebConstants.CURRENT_COMPANY).toString());
		ArrayList<HashMap<String, Object>> list = ruleService
				.getExportId(listParam);
		String Id_Rule = "";
		for (HashMap<String, Object> map : list) {
			Id_Rule =Id_Rule+ map.get("rule_id").toString() + "@";
		}
		try {
			if (WebConstants.RULE_TYPE_RETURN.equals(rule_type)) {
				// 退货模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_RETURN + "_"
							+ WebConstants.RULE1_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_RETURN + "_"
							+ WebConstants.RULE1_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getRuleMainData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getAdjustAfterRuleData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeRuleMainSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeAdjustAfterRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_RETURN + "_"
							+ WebConstants.RULE1_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_RETURN + "_"
							+ WebConstants.RULE1_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_DISCOUN.equals(rule_type)) {
				// 折扣模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_DISCOUN + "_"
							+ WebConstants.RULE2_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_DISCOUN + "_"
							+ WebConstants.RULE2_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService
							.getRuleDiscountData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getAdjustAfterRuleData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeDiscountSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeAdjustAfterRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_DISCOUN + "_"
							+ WebConstants.RULE2_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_DISCOUN + "_"
							+ WebConstants.RULE2_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_REBATE.equals(rule_type)) {
				// 折扣模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REBATE + "_"
							+ WebConstants.RULE3_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REBATE + "_"
							+ WebConstants.RULE3_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getRuleRebateData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getAdjustAfterRuleData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeRebateSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeAdjustAfterRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_REBATE + "_"
							+ WebConstants.RULE3_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_REBATE + "_"
							+ WebConstants.RULE3_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_SPLIT.equals(rule_type)) {
				// 拆分模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_SPLIT + "_"
							+ WebConstants.RULE4_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_SPLIT + "_"
							+ WebConstants.RULE4_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getRuleSplitData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getAdjustAfterRuleData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeSplitSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeAdjustAfterRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_SPLIT + "_"
							+ WebConstants.RULE4_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_SPLIT + "_"
							+ WebConstants.RULE4_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_MERGE.equals(rule_type)) {
				// 发票分组模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_MERGE + "_"
							+ WebConstants.RULE5_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_MERGE + "_"
							+ WebConstants.RULE5_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getRuleMainData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getAdjustAfterRuleData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeRuleMainSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeAdjustAfterRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_MERGE + "_"
							+ WebConstants.RULE5_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_MERGE + "_"
							+ WebConstants.RULE5_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_WITHDETAILS.equals(rule_type)) {
				// 合并明细模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_WITHDETAILS + "_"
							+ WebConstants.RULE6_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_WITHDETAILS + "_"
							+ WebConstants.RULE6_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getRuleMainData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getAdjustAfterRuleData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeRuleMainSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeAdjustAfterRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_WITHDETAILS + "_"
							+ WebConstants.RULE6_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_WITHDETAILS + "_"
							+ WebConstants.RULE6_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_REPLACE_GOOD.equals(rule_type)) {
				// 替换商品名模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_GOOD + "_"
							+ WebConstants.RULE7_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_GOOD + "_"
							+ WebConstants.RULE7_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService
							.getreplaceOthersData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeplaceOthersSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_GOOD + "_"
							+ WebConstants.RULE7_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_GOOD + "_"
							+ WebConstants.RULE7_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_REPLACE_CUSTOMER
					.equals(rule_type)) {
				// 替换客户名模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_CUSTOMER + "_"
							+ WebConstants.RULE8_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_CUSTOMER + "_"
							+ WebConstants.RULE8_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService
							.getreplaceOthersData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeplaceOthersSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_CUSTOMER + "_"
							+ WebConstants.RULE8_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_CUSTOMER + "_"
							+ WebConstants.RULE8_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_REPLACE_UNIT.equals(rule_type)) {
				// 替换单位模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_UNIT + "_"
							+ WebConstants.RULE9_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_UNIT + "_"
							+ WebConstants.RULE9_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data
							.addAll(ruleService.getreplaceUnitData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeplaceUnitSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_UNIT + "_"
							+ WebConstants.RULE9_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_UNIT + "_"
							+ WebConstants.RULE9_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_REPLACE_TYPE.equals(rule_type)) {
				// 替换规格模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_TYPE + "_"
							+ WebConstants.RULE10_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REPLACE_TYPE + "_"
							+ WebConstants.RULE10_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService
							.getreplaceOthersData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeplaceOthersSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_TYPE + "_"
							+ WebConstants.RULE10_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_REPLACE_TYPE + "_"
							+ WebConstants.RULE10_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_ABSTRACT.equals(rule_type)) {
				// 自定义摘要模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_ABSTRACT + "_"
							+ WebConstants.RULE11_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_ABSTRACT + "_"
							+ WebConstants.RULE11_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getRuleMainData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getCustomizeRemarkData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeRuleMainSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeCustomizeRemarkRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_ABSTRACT + "_"
							+ WebConstants.RULE11_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_ABSTRACT + "_"
							+ WebConstants.RULE11_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_REMARK.equals(rule_type)) {
				// 自定义备注模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REMARK + "_"
							+ WebConstants.RULE12_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_REMARK + "_"
							+ WebConstants.RULE12_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				adjust_after_rule_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getRuleMainData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
					// 规则后调整
					adjust_after_rule_data.addAll(ruleService
							.getCustomizeRemarkData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeRuleMainSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);

				// 规则处理内容sheet
				HSSFSheet sheet3 = workbook.getSheetAt(2);
				writeCustomizeRemarkRule(sheet3);

				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_REMARK + "_"
							+ WebConstants.RULE12_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_REMARK + "_"
							+ WebConstants.RULE12_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			} else if (WebConstants.RULE_TYPE_DOCUMENT_CONSERVATION
					.equals(rule_type)) {
				// 单据保持模板
				// ------------------------- 导入模板
				// 获取流，读取workbook
				if(language.equals("en")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_DOCUMENT_CONSERVATION + "_"
							+ WebConstants.RULE13_EN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}
				else if(language.equals("zh_CN")){
					is = new FileInputStream(strPath
							+ WebConstants.RULE_TYPE_DOCUMENT_CONSERVATION + "_"
							+ WebConstants.RULE13_CN + ".xls");
					poifs = new POIFSFileSystem(is);
					workbook = new HSSFWorkbook(poifs);
				}

				// ------------------------- 查询数据
				// 实例化查询参数
				rule_main_data = new ArrayList<HashMap<String, Object>>();
				rule_condition_data = new ArrayList<HashMap<String, Object>>();
				// 查询数据
				HashMap<String, Object> param = new HashMap<String, Object>();
				String[] strs = Id_Rule.split("@");
				for (String string : strs) {
					param.put("id", string);
					// 规则主数据数据
					rule_main_data.addAll(ruleService.getDocTypeData(param));
					// 规则条件
					rule_condition_data.addAll(ruleService
							.getRuleConditionData(param));
				}

				// ------------------------- 写入模板
				// 规则主数据sheet
				HSSFSheet sheet1 = workbook.getSheetAt(0);
				writeDocumentStateSheet(sheet1);

				// 规则条件项目sheet
				HSSFSheet sheet2 = workbook.getSheetAt(1);
				writeRuleConditionSheet(sheet2, lang);
				// ------------------------- 导出模板
				// 设置导出文件名和编码
				if(language.equals("en")){
					String fileName = WebConstants.RULE_TYPE_DOCUMENT_CONSERVATION + "_"
							+ WebConstants.RULE13_EN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
				}else if(language.equals("zh_CN")){
					String fileName = WebConstants.RULE_TYPE_DOCUMENT_CONSERVATION + "_"
							+ WebConstants.RULE13_CN + ".xls";
					response.setHeader("Content-Disposition", "filename="
							+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
				}
				// 输出
				out = response.getOutputStream();
				workbook.write(out);
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.setCharacterEncoding("GBK");
				if(language.equals("en")){
					response.setHeader("Content-Disposition", "filename="+"Error list.xls");
					response.getWriter().write("Export operation failed, please check the configuration of your local model.");
				}else if(language.equals("zh_CN")){
					response.setHeader("Content-Disposition", "filename="+URLEncoder.encode("错误列表", "UTF-8")+".xls");
					response.getWriter().write("导出失败，请检查模板配置。");
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			// ------------------------- 关闭流
			closeStream();
		}
	}

	/**
	 * 自定义摘要、备注规则
	 * 
	 * @param adjustSheet
	 */
	private void writeCustomizeRemarkRule(HSSFSheet adjustSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : adjust_after_rule_data) {
			// 新建一行
			HSSFRow datarow = adjustSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 自定义项目
					entry.get("CUS_COLUMN").toString() + ":"
							+ entry.get("PROJECT_DESC_CHN").toString());
			datarow.createCell(2).setCellValue( // 自定义类型
					changeCusType(entry.get("CUS_TYPE").toString(), lang));
			datarow.createCell(3).setCellValue( // 截取前后FLG
					changeFixType(entry.get("FIXED_TYPE").toString(), lang));
			if (!"0".equals(entry.get("FIXED_LEN").toString())) {
				datarow.createCell(4).setCellValue( // 截取长度
						entry.get("FIXED_LEN").toString());
			}
			datarow.createCell(5).setCellValue( // 前后文本
					entry.get("BEFORE_FIXED_CHAR").toString());
			datarow.createCell(6)
					.setCellValue(
							// 前后文本FLG
							changeLocationFlg(entry.get("BEFORE_AFTER_TYPE")
									.toString(), lang));
			datarow.createCell(7).setCellValue( // 固定值
					entry.get("FIXED_TEXT").toString());
		}
	}

	/**
	 * 写替换商品、客户、规格规则主数据sheet
	 * 
	 * @param sheet1
	 */
	private void writeplaceUnitSheet(HSSFSheet ruleMainSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_main_data) {
			// 新建一行
			HSSFRow datarow = ruleMainSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 公司代码
					entry.get("COMPANY_CODE").toString());
			datarow.createCell(2).setCellValue( // 客户代码
					entry.get("CUST_CODE").toString());
			datarow.createCell(3).setCellValue( // 有效范围
					changeScope(entry.get("VALID_FLG").toString(),lang));
			datarow.createCell(4).setCellValue( // 备注
					entry.get("REMARKS").toString());
			datarow.createCell(5).setCellValue( // 替换前单位
					entry.get("UNIT_BEFORE").toString());
			if (entry.get("REPLACE_RATE") != null) {
				datarow.createCell(6).setCellValue( // 替换率
						entry.get("REPLACE_RATE").toString());
			}
			datarow.createCell(7).setCellValue( // 替换后单位
					entry.get("UNIT_AFTER").toString());
		}
	}
	/**
	 * 写单据状态主数据Sheet
	 *
	 */
	private void writeDocumentStateSheet(HSSFSheet docStateSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_main_data) {
			// 新建一行
			HSSFRow datarow = docStateSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 公司代码
					entry.get("COMPANY_CODE").toString());
			datarow.createCell(2).setCellValue( // 客户代码
					entry.get("CUST_CODE").toString());
			datarow.createCell(3).setCellValue( // 有效范围
					changeScope(entry.get("VALID_FLG").toString(), lang));
			datarow.createCell(4).setCellValue( // 备注
					entry.get("REMARKS").toString());
			datarow.createCell(5).setCellValue( // 单据状态
					changeState((entry.get("DOC_STATUS") == null?"":entry.get("DOC_STATUS")).toString(), lang));
			datarow.createCell(6).setCellValue( // 单据保持FLAG
					chageLock(entry.get("LOCK_FLAG").toString(),lang));
		} 
		
	}
	
	// 修改是否保持
	private String chageLock(String lock_flg, String language) {
		if(language.equals("zh_CN")){
			if ("0".equals(lock_flg)) {
				return "0:未保持";
			}else if("1".equals(lock_flg)){
				return "1:已保持";
			}else{
				return "";
			}
		}else if(language.equals("en")){
			if ("0".equals(lock_flg)) {
				return "0:Not hold";
			}else if("1".equals(lock_flg)){
				return "1:Hold";
			}else{
				return "";
			}
		}else{
			return "";
		}
	}
	// 修改状态显示
	private String changeState(String doc_state, String language) {
		if(language.equals("zh_CN")){
			if ("0".equals(doc_state)) {
				return "0:未开票";
			} else if ("1".equals(doc_state)) {
				return "1:已开票";
			} else if ("2".equals(doc_state)) {
				return "2:打印中";
			} else if ("3".equals(doc_state)) {
				return "3:已审核";
			} else if ("4".equals(doc_state)) {
				return "4:已导出";
			} else if ("5".equals(doc_state)) {
				return "5:已红冲";
			} else if ("6".equals(doc_state)) {
				return "6:已拆分";
			} else if ("7".equals(doc_state)) {
				return "7:已合并";
			}else if ("8".equals(doc_state)) {
				return "8:已关联";
			} else if ("9".equals(doc_state)) {
				return "9:已回写";
			} else if ("10".equals(doc_state)) {
				return "10:已寄出";
			} else if ("11".equals(doc_state)) {
				return "11:已送信";
			}else {
				return "";
			}
		}else if(language.equals("en")){
			if ("0".equals(doc_state)) {
				return "0:The invoice has not been drawn";
			} else if ("1".equals(doc_state)) {
				return "1:Invoice has been drawn";
			} else if ("2".equals(doc_state)) {
				return "2:Printing";
			} else if ("3".equals(doc_state)) {
				return "3:Reviewed";
			} else if ("4".equals(doc_state)) {
				return "4:Exported";
			} else if ("5".equals(doc_state)) {
				return "5:It has been return.";
			} else if ("6".equals(doc_state)) {
				return "6:Split";
			} else if ("7".equals(doc_state)) {
				return "7:Consolidated";
			}else if ("8".equals(doc_state)) {
				return "8:Associated";
			} else if ("9".equals(doc_state)) {
				return "9:Write back";
			} else if ("10".equals(doc_state)) {
				return "10:Sent";
			} else if ("11".equals(doc_state)) {
				return "11:Informed";
			}else {
				return "";
			}
		}else{
			return "";
		}
	}

	/**
	 * 写替换商品、客户、规格规则主数据sheet
	 * 
	 * @param ruleMainSheet
	 */
	private void writeplaceOthersSheet(HSSFSheet ruleMainSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_main_data) {
			// 新建一行
			HSSFRow datarow = ruleMainSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 公司代码
					entry.get("COMPANY_CODE").toString());
			datarow.createCell(2).setCellValue( // 客户代码
					entry.get("CUST_CODE").toString());
			datarow.createCell(3).setCellValue( // 有效范围
					changeScope(entry.get("VALID_FLG").toString(), lang));
			datarow.createCell(4).setCellValue( // 备注
					entry.get("REMARKS").toString());
			datarow.createCell(5).setCellValue( // 替换成
					entry.get("REPLACE_TEXT").toString());
		}
	}

	/**
	 * 写拆分表的规则主数据sheet
	 * 
	 * @param ruleMainSheet
	 */
	private void writeSplitSheet(HSSFSheet ruleMainSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_main_data) {
			// 新建一行
			HSSFRow datarow = ruleMainSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 公司代码
					entry.get("COMPANY_CODE").toString());
			datarow.createCell(2).setCellValue( // 客户代码
					entry.get("CUST_CODE").toString());
			datarow.createCell(3).setCellValue( // 有效范围
					changeScope(entry.get("VALID_FLG").toString(),lang));
			datarow.createCell(4).setCellValue( // 备注
					entry.get("REMARKS").toString());
			datarow.createCell(5).setCellValue( // 拆分方式
					changeSplitType(entry.get("SPLIT_TYPE").toString(),lang));
			if (entry.get("TOP_AMOUNT") != null) {
				datarow.createCell(6).setCellValue( // 最大金额
						entry.get("TOP_AMOUNT").toString());
			}
			if (entry.get("SPLIT_RATE_1") != null) {
				datarow.createCell(7).setCellValue( // 拆分比率1
						entry.get("SPLIT_RATE_1").toString());
			}
			if (entry.get("SPLIT_RATE_2") != null) {
				datarow.createCell(8).setCellValue( // 拆分比率2
						entry.get("SPLIT_RATE_2").toString());
			}
			if (entry.get("SPLIT_RATE_3") != null) {
				datarow.createCell(9).setCellValue( // 拆分比率3
						entry.get("SPLIT_RATE_3").toString());
			}
			if (entry.get("SPLIT_RATE_4") != null) {
				datarow.createCell(10).setCellValue( // 拆分比率4
						entry.get("SPLIT_RATE_4").toString());
			}
			if (entry.get("SPLIT_RATE_5") != null) {
				datarow.createCell(11).setCellValue( // 拆分比率5
						entry.get("SPLIT_RATE_5").toString());
			}
		}
	}

	/**
	 * 写返利表的规则主数据sheet
	 * 
	 * @param ruleMainSheet
	 */
	private void writeRebateSheet(HSSFSheet ruleMainSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_main_data) {
			// 新建一行
			HSSFRow datarow = ruleMainSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 公司代码
					entry.get("COMPANY_CODE").toString());
			datarow.createCell(2).setCellValue( // 客户代码
					entry.get("CUST_CODE").toString());
			datarow.createCell(3).setCellValue( // 有效范围
					changeScope(entry.get("VALID_FLG").toString(),lang));
			datarow.createCell(4).setCellValue( // 备注
					entry.get("REMARKS").toString());
			if (entry.get("REBATE_TOTAL") != null) {
				datarow.createCell(5).setCellValue( // 返利总额
						entry.get("REBATE_TOTAL").toString());
			}
			datarow.createCell(6).setCellValue( // 含税FLAG
					changeTaxName(entry.get("PRICE_WAY").toString(), lang));
			if (entry.get("REBATE_BALANCE") != null) {
				datarow.createCell(7).setCellValue( // 返利余额
						entry.get("REBATE_BALANCE").toString());
			}
//			datarow.createCell(7).setCellValue( // 重置FLAG
//					changeResetFlg(entry.get("REBATE_RESET_FLG").toString()));
			datarow.createCell(8).setCellValue( // 有效期间 FROM
					entry.get("TIME_START").toString());
			datarow.createCell(9).setCellValue( // 有效期间TO
					entry.get("TIME_END").toString());
			if (entry.get("REBATE_RATE") != null) {
				datarow.createCell(10).setCellValue( // 返利率
						entry.get("REBATE_RATE").toString());
			}
		}

	}
	
	/**
	 *是否含税FLAG 
	 * 
	 */
	private String changeTaxName(String priceWay, String language) {
		if(language.equals("zh_CN")){
			if("0".equals(priceWay)){
				return "0:不含税";
			}else{
				return "1:含税";
			}
		}else if(language.equals("en")){
			if("0".equals(priceWay)){
				return "0:Excluding tax";
			}else{
				return "1:Incl. tax";
			}
		}else{
			return "";
		}
	}

	/**
	 * 写折扣表的规则主数据sheet
	 * 
	 * @param ruleMainSheet
	 */
	private void writeDiscountSheet(HSSFSheet ruleMainSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_main_data) {
			// 新建一行
			HSSFRow datarow = ruleMainSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 公司代码
					entry.get("COMPANY_CODE").toString());
			datarow.createCell(2).setCellValue( // 客户代码
					entry.get("CUST_CODE").toString());
			datarow.createCell(3).setCellValue( // 有效范围
					changeScope(entry.get("VALID_FLG").toString(),lang));
			datarow.createCell(4).setCellValue( // 备注
					entry.get("REMARKS").toString());
			if (entry.get("DISCOUNT_LIMIT") != null) {
				datarow.createCell(5).setCellValue( // 折扣上限
						entry.get("DISCOUNT_LIMIT").toString());
			}
		}
	}

	/**
	 * 写规则主数据sheet
	 * 
	 * @param ruleMainSheet
	 */
	private void writeRuleMainSheet(HSSFSheet ruleMainSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_main_data) {
			// 新建一行
			HSSFRow datarow = ruleMainSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue( // 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue( // 公司代码
					entry.get("COMPANY_CODE").toString());
			datarow.createCell(2).setCellValue( // 客户代码
					entry.get("CUST_CODE").toString());
			datarow.createCell(3).setCellValue( // 有效范围
					changeScope(entry.get("VALID_FLG").toString(),lang));
			datarow.createCell(4).setCellValue(entry.get("REMARKS").toString());
		}
	}

	/**
	 * 写规则条件项目sheet
	 * 
	 * @param ruleConditionSheet
	 */
	private void writeRuleConditionSheet(HSSFSheet ruleConditionSheet, String language) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : rule_condition_data) {
			// 新建一行
			HSSFRow datarow = ruleConditionSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue(// 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			if(language.equals("zh_CN")){
			datarow.createCell(1).setCellValue(// 条件项目(中文)
					entry.get("CONDITION_COLUMN").toString() + ":"
							+ entry.get("PROJECT_DESC_CHN").toString());
			}else if(language.equals("en")){
				datarow.createCell(1).setCellValue(// 条件项目(英文)
						entry.get("CONDITION_COLUMN").toString()+ ":"
								+ entry.get("CONDITION_COLUMN").toString());
			}
			datarow.createCell(2).setCellValue(// 条件
					changeCondition(entry.get("CONDITION").toString(), lang));
			datarow.createCell(3).setCellValue(// 值
					entry.get("VALUE").toString());
		}
	}

	/**
	 * 写规则处理内容sheet
	 * 
	 * @param adjustAfterRuleSheet
	 */
	private void writeAdjustAfterRule(HSSFSheet adjustAfterRuleSheet) {
		// 开始写的行数
		int rowNum = 2;
		for (HashMap<String, Object> entry : adjust_after_rule_data) {
			// 新建一行
			HSSFRow datarow = adjustAfterRuleSheet.createRow(rowNum++);
			datarow.createCell(0).setCellValue(// 规则ID
					entry.get("RULE_DETAIL_ID").toString());
			datarow.createCell(1).setCellValue(// 调整项目
					changeAdjustColumn(entry.get("ADJUST_COLUMN").toString(), lang));
			datarow.createCell(2).setCellValue(// 调整方式
					changeAdjustType(entry.get("ADJUST_TYPE").toString(), lang));
			datarow.createCell(3).setCellValue(// 固定值
					entry.get("FIXED_TEXT").toString());
		}
	}

	/**
	 * 关闭流
	 */
	private void closeStream() {
		try {
			if (null != is) {
				is.close();
			}
			if (null != out) {
				out.flush();
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 改变有效范围的导出值
	 * 
	 * @param scope
	 * @param lang 
	 * @return
	 */
	private String changeScope(String scope, String language) {
		if(language.equals("en")){
			if ("0".equals(scope)) {
				return "0:Work on current customer";
			} else if ("1".equals(scope)) {
				return "1:Work on all customers";
			} else if ("2".equals(scope)) {
				return "2:Not work on current customer";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("0".equals(scope)) {
				return "0:该规则对当前客户有效";
			} else if ("1".equals(scope)) {
				return "1:该规则对全部客户有效";
			} else if ("2".equals(scope)) {
				return "2:当前客户不启用该规则";
			} else {
				return "";
			}
		}else{
			return "";
		}
	}

	/**
	 * 改变条件的导出值
	 * 
	 * @param condition
	 * @return
	 */
	private String changeCondition(String condition, String language) {
		if(language.equals("en")){
			if ("01".equals(condition)) {
				return "01:=";
			} else if ("02".equals(condition)) {
				return "02:>";
			} else if ("03".equals(condition)) {
				return "03:<";
			} else if ("04".equals(condition)) {
				return "04:Contains";
			} else if ("05".equals(condition)) {
				return "05:Start with";
			} else if ("06".equals(condition)) {
				return "06:End with";
			} else if ("10".equals(condition)) {
				return "10:Equals(Match)";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("01".equals(condition)) {
				return "01:=";
			} else if ("02".equals(condition)) {
				return "02:>";
			} else if ("03".equals(condition)) {
				return "03:<";
			} else if ("04".equals(condition)) {
				return "04:包含";
			} else if ("05".equals(condition)) {
				return "05:开头是";
			} else if ("06".equals(condition)) {
				return "06:结尾是";
			} else if ("10".equals(condition)) {
				return "10:相等(匹配)";
			} else {
				return "";
			}
		}else{
			return "";
		}
	}

	/**
	 * 改变调整项目导出值
	 * 
	 * @param adjustColumn
	 * @return
	 */
	private String changeAdjustColumn(String adjustColumn, String language) {
		if(language.equals("en")){
			if ("D_QUANTITY".equals(adjustColumn)) {
				return "D_QUANTITY:Quantity";
			} else if ("D_PRICE_CUR".equals(adjustColumn)) {
				return "D_PRICE_CUR:Price";
			} else if ("D_GOODS_NAME".equals(adjustColumn)) {
				return "D_GOODS_NAME:Goods name";
			} else if ("D_SPEC".equals(adjustColumn)) {
				return "D_SPEC:Specifications";
			} else if ("D_GOODS_UNIT".equals(adjustColumn)) {
				return "D_GOODS_UNIT:Unit";
			} else if ("H_REMARK".equals(adjustColumn)) {
				return "H_REMARK:Remark";
			} else if ("D_REMARK".equals(adjustColumn)) {
				return "D_REMARK:Abstract";
			} else if ("D_DISCOUNT_RATE".equals(adjustColumn)) {
				return "D_DISCOUNT_RATE:Discount rate";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("D_QUANTITY".equals(adjustColumn)) {
				return "D_QUANTITY:数量";
			} else if ("D_PRICE_CUR".equals(adjustColumn)) {
				return "D_PRICE_CUR:单价";
			} else if ("D_GOODS_NAME".equals(adjustColumn)) {
				return "D_GOODS_NAME:商品名";
			} else if ("D_SPEC".equals(adjustColumn)) {
				return "D_SPEC:规格";
			} else if ("D_GOODS_UNIT".equals(adjustColumn)) {
				return "D_GOODS_UNIT:单位";
			} else if ("H_REMARK".equals(adjustColumn)) {
				return "H_REMARK:备注";
			} else if ("D_REMARK".equals(adjustColumn)) {
				return "D_REMARK:摘要";
			} else if ("D_DISCOUNT_RATE".equals(adjustColumn)) {
				return "D_DISCOUNT_RATE:折扣率";
			} else {
				return "";
			}
		}else{
			return "";
		}
	}

	/**
	 * 改变调整方式导出值
	 * 
	 * @param adjustType
	 * @return
	 */
	private String changeAdjustType(String adjustType,String language) {
		if(language.equals("en")){
			if ("00".equals(adjustType)) {
				return "00:Unchanging";
			} else if ("01".equals(adjustType)) {
				return "01:Add";
			} else if ("02".equals(adjustType)) {
				return "02:Max";
			} else if ("03".equals(adjustType)) {
				return "03:Min";
			} else if ("04".equals(adjustType)) {
				return "04:First line";
			} else if ("05".equals(adjustType)) {
				return "05:Merge";
			} else if ("06".equals(adjustType)) {
				return "06:Calculation";
			} else if ("07".equals(adjustType)) {
				return "07:Fixed value";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("00".equals(adjustType)) {
				return "00:不变";
			} else if ("01".equals(adjustType)) {
				return "01:相加";
			} else if ("02".equals(adjustType)) {
				return "02:最大";
			} else if ("03".equals(adjustType)) {
				return "03:最小";
			} else if ("04".equals(adjustType)) {
				return "04:首行";
			} else if ("05".equals(adjustType)) {
				return "05:合并";
			} else if ("06".equals(adjustType)) {
				return "06:反算";
			} else if ("07".equals(adjustType)) {
				return "07:固定值";
			} else {
				return "";
			}
		}else{
			return "";
		}
	}

	/**
	 * 改变重置导出值
	 * 
	 * @param resetFlg
	 * @return
	 */
	private String changeResetFlg(String resetFlg) {
		if ("0".equals(resetFlg)) {
			return "0:不重置";
		} else if ("1".equals(resetFlg)) {
			return "1:重置";
		} else {
			return "";
		}
	}

	/**
	 * 改变拆分方式导出值
	 * 
	 * @param splitType
	 * @return
	 */
	private String changeSplitType(String splitType, String language) {
		if(language.equals("en")){
			if ("0".equals(splitType)) {
				return "0:Maximum amount split way";
			} else if ("1".equals(splitType)) {
				return "1:Fixed rate split way";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("0".equals(splitType)) {
				return "0:按最大金额";
			} else if ("1".equals(splitType)) {
				return "1:按固定比率";
			} else {
				return "";
			}
		}else{
			return "";
		}
		
	}

	/**
	 * 改变自定义类型导出值
	 * 
	 * @param cusType
	 * @return
	 */
	private String changeCusType(String cusType,String language) {
		if(language.equals("en")){
			if ("0".equals(cusType)) {
				return "0:Length intercepting way";
			} else if ("1".equals(cusType)) {
				return "1:Text before you set value";
			} else if ("2".equals(cusType)) {
				return "2:Fixed text";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("0".equals(cusType)) {
				return "0:按长度截取";
			} else if ("1".equals(cusType)) {
				return "1:某字符之前的文本";
			} else if ("2".equals(cusType)) {
				return "2:固定文本";
			} else {
				return "";
			}
		}else{
		   return "";
		}
	}

	/**
	 * 改变截取前后FLG导出值
	 * 
	 * @param fixType
	 * @return
	 */
	private String changeFixType(String fixType, String language) {
		if(language.equals("en")){
			if ("0".equals(fixType)) {
				return "0:Intercept form start";
			} else if ("1".equals(fixType)) {
				return "1:Intercept from end";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("0".equals(fixType)) {
				return "0:从前开始截";
			} else if ("1".equals(fixType)) {
				return "1:从后开始截";
			} else {
				return "";
			}
		}else{
		   return "";
		}
		
	}

	/**
	 * 改变截取前后位置FLG导出值
	 * 
	 * @param locationFlg
	 * @return
	 */
	private String changeLocationFlg(String locationFlg, String language) {
		if(language.equals("en")){
			if ("0".equals(locationFlg)) {
				return "0:Text before this value";
			} else if ("1".equals(locationFlg)) {
				return "1:text after this value";
			} else {
				return "";
			}
		}else if(language.equals("zh_CN")){
			if ("0".equals(locationFlg)) {
				return "0:字符前文本";
			} else if ("1".equals(locationFlg)) {
				return "1:字符后文本";
			} else {
				return "";
			}
		}else{
		   return "";
		}
	}

}
